package com.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.bean.*;
import com.movie.bean.vo.UserQueryVO;
import com.movie.enums.OrderStatus;
import com.movie.mapper.RechargeMapper;
import com.movie.mapper.TicketMapper;
import com.movie.mapper.UserMapper;
import com.movie.service.MovieService;
import com.movie.service.UserService;
import com.movie.utils.BeanUtil;
import com.movie.utils.OrderNumberGenerator;
import com.movie.utils.PageUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private MovieService movieService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void HandleUserRegister(User user) {
        userMapper.HandleUserRegister(user);
    }

    @Override
    public int CheckName(String name) {
        return userMapper.CheckName(name);
    }

    @Override
    public User CheckLogin(String uname, String upassword) {
        return userMapper.CheckLogin(uname, upassword);
    }

    @Override
    public List<TicketOrder> getUserPaidTicketList(int uid) {
        return userMapper.getUserPaidTicketList(uid);
    }

    @Override
    public User getUserByUid(int uid) {
        return userMapper.getUserByUid(uid);
    }

    @Override
    public void updateBalance(double money, int uid) {
        Recharge recharge = new Recharge();
        recharge.setUid(uid);
        recharge.setMoney(money);
        recharge.setNumber(OrderNumberGenerator.generateOrderNumber());
        BeanUtil.setDefault(recharge);
        rechargeMapper.insert(recharge);
        userMapper.updateBalance(money, uid);
    }

    @Override
    public List<TicketOrder> geTWaitPaidUserOrderList(int uid) {


        return userMapper.geTWaitPaidUserOrderList(uid);
    }


    @Override
    @Transactional
    public void updateTicketStatus(String number, int total, int uid,List<Ticket> ticketList) { //支付电影订单
        movieService.buyTicket(ticketList);
        userMapper.subBalance(total, uid);
    }

    @Override
    public void handleDeleteUserOrder(String number,Integer status) {
        OrderStatus orderEnum = OrderStatus.getOrderEnum(status);
        switch (orderEnum){
            case PAID:
            case REFUND:
                ticketMapper.delete(number);
                break;
            case UNPAID:
                LinkedHashSet<String> key= (LinkedHashSet<String>) redisTemplate.keys("*:"+number);//删除该场次对应的订单
                redisTemplate.delete(key);
                key= (LinkedHashSet<String>) redisTemplate.keys("*:"+number+":*");//删除该场次的对应订单的座位号
                redisTemplate.delete(key);
                break;
        }
//        userMapper.handleDeleteUserOrder(number);
    }

    @Override
    public void addComment(Comment comment) {
        userMapper.addComment(comment);
    }

    @Override
    public List<Comment> getCommentList(int mid) {
        return userMapper.getCommentList(mid);
    }

    @Override
    public void delLike(int uid,int cid) {
        userMapper.delLike(uid,cid);
    }

    @Override
    public void addLike(int uid, int cid) {
        userMapper.addLike(uid, cid);
    }

    @Override
    public CommentLike isExistCommentLike(int uid, int cid) {
        return userMapper.isExistCommentLike(uid,cid);
    }

    @Override
    public void addFavorite(Favorite favorite) {
        userMapper.addFavorite(favorite);
    }

    @Override
    public void delFavorite(Favorite favorite) {
        userMapper.delFavorite(favorite);
    }

    @Override
    public Favorite isExistFavorite(int uid, int mid) {
        return userMapper.isExistFavorite(uid,mid);
    }

    @Override
    public User updateInfo(User user) {
        userMapper.updateInfo(user);
        return user;
    }

    @Override
    public int checkedComment(int mid, int uid) {
        return userMapper.checkedComment(mid,uid);
    }

    @Override
    public Page<User> userPage(UserQueryVO userQueryVO) {
        List<User> users = userMapper.selectList(userQueryVO);
        return PageUtil.PaginatedResult(users,userQueryVO.getPageNo(),userQueryVO.getPageSize());
    }

    @Override
    public void banUser(User user) {
        userMapper.banUser(user);
    }
}
